package com.cskaoyan.converter;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.AdminUsedByTHZ;
import com.cskaoyan.bean.pojo.Permission;
import com.cskaoyan.bean.pojo.Role;
import com.cskaoyan.bean.pojo.Storage;
import com.cskaoyan.bean.vo.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: thz
 * @class: SystemConverter
 * @desc: 系统模块bo，vo，pojo的相关转换
 */
public class SystemConverter {
    public static AdminUsedByTHZ convertToAdmin(AdminCreateBO adminCreateBO) {
        AdminUsedByTHZ admin = new AdminUsedByTHZ();
        admin.setUsername(adminCreateBO.getUsername());
        admin.setPassword(adminCreateBO.getPassword());
        admin.setRoleIds(String.valueOf(adminCreateBO.getRoleIds()));
        return admin;
    }

    public static AdminCreateVO convertToAdminCreateVO(AdminUsedByTHZ admin) {
        AdminCreateVO adminCreateVO = new AdminCreateVO();
        adminCreateVO.setId(admin.getId());
        adminCreateVO.setUsername(admin.getUsername());
        adminCreateVO.setPassword(admin.getPassword());
        adminCreateVO.setAddTime(admin.getAddTime());
        adminCreateVO.setUpdateTime(admin.getUpdateTime());

        String[] split = admin.getRoleIds().substring(1, admin.getRoleIds().length() - 1).split(",");
        List<Integer> collect = new ArrayList<>();
        if (!"".equals(split[0])) {
            collect = Arrays.stream(split)
                    .map(s -> Integer.parseInt(s.trim()))
                    .collect(Collectors.toList());
        }
        adminCreateVO.setRoleIds(collect);
        adminCreateVO.setAvatar(admin.getAvatar());
        return adminCreateVO;
    }

    public static AdminUsedByTHZ convertToAdmin(AdminUpdateBO adminUpdateBO) {
        AdminUsedByTHZ admin = new AdminUsedByTHZ();
        admin.setUpdateTime(new Date());
        admin.setId(adminUpdateBO.getId());
        admin.setUsername(adminUpdateBO.getUsername());
        admin.setPassword(adminUpdateBO.getPassword());
        admin.setAvatar(adminUpdateBO.getAvatar());
        admin.setRoleIds(Arrays.toString(adminUpdateBO.getRoleIds().toArray()));
        return admin;
    }

    public static AdminUpdateVo convertToAdminUpdateVO(AdminUsedByTHZ admin) {
        AdminUpdateVo adminUpdateVo = new AdminUpdateVo();
        adminUpdateVo.setId(admin.getId());
        adminUpdateVo.setUsername(admin.getUsername());
        adminUpdateVo.setPassword(admin.getPassword());
        adminUpdateVo.setAvatar(admin.getAvatar());
        adminUpdateVo.setUpdateTime(admin.getUpdateTime());
        String[] split = admin.getRoleIds().substring(1, admin.getRoleIds().length() - 1).split(",");
        List<Integer> collect = new ArrayList<>();
        if (!"".equals(split[0])) {
            collect = Arrays.stream(split)
                    .map(s -> Integer.parseInt(s.trim()))
                    .collect(Collectors.toList());
        }
        adminUpdateVo.setRoleIds(collect);
        return adminUpdateVo;
    }

    public static AdminUsedByTHZ convertToAdmin(AdminDeleteBO adminDeleteBO) {
        AdminUsedByTHZ admin = new AdminUsedByTHZ();
        admin.setId(adminDeleteBO.getId());
        admin.setUsername(adminDeleteBO.getUsername());
        admin.setAvatar(adminDeleteBO.getAvatar());
        admin.setDeleted(true);
        admin.setUpdateTime(new Date());
        admin.setRoleIds(String.valueOf(adminDeleteBO.getRoleIds()));
        return admin;
    }

    public static Role convertToRole(RoleCreateBO roleCreateBO) {
        Role role = new Role();
        role.setName(roleCreateBO.getName());
        role.setDesc(roleCreateBO.getDesc());
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        return role;
    }

    public static RoleCreateVO convertToRoleCreateVO(Role role) {
        RoleCreateVO roleCreateVO = new RoleCreateVO();
        roleCreateVO.setId(role.getId());
        roleCreateVO.setId(role.getId());
        roleCreateVO.setName(role.getName());
        roleCreateVO.setDesc(role.getDesc());
        roleCreateVO.setAddTime(role.getAddTime());
        roleCreateVO.setUpdateTime(role.getUpdateTime());
        return roleCreateVO;
    }

    public static Role convertToRole(RoleDeleteBO roleDeleteBO) {
        Role role = new Role();
        role.setId(roleDeleteBO.getId());
        role.setName(roleDeleteBO.getName());
        role.setDesc(roleDeleteBO.getDesc());
        role.setEnabled(roleDeleteBO.isEnabled());
        role.setAddTime(roleDeleteBO.getAddTime());
        role.setUpdateTime(new Date());
        role.setDeleted(roleDeleteBO.isDeleted());
        role.setDeleted(true);
        return role;
    }

    public static Role convertToRole(RoleUpdateBO roleUpdateBO) {
        Role role = new Role();
        role.setId(roleUpdateBO.getId());
        role.setName(roleUpdateBO.getName());
        role.setDesc(roleUpdateBO.getDesc());
        role.setAddTime(roleUpdateBO.getAddTime());
        role.setUpdateTime(new Date());
        return role;
    }

    public static Storage convertToStorage(StorageUpdateBo storageUpdateBo) {
        Storage storage = new Storage();
        storage.setId(storageUpdateBo.getId());
        storage.setKey(storageUpdateBo.getKey());
        storage.setName(storageUpdateBo.getName());
        storage.setType(storageUpdateBo.getType());
        storage.setSize(storageUpdateBo.getSize());
        storage.setUrl(storageUpdateBo.getUrl());
        storage.setAddTime(storageUpdateBo.getAddTime());
        storage.setUpdateTime(new Date());
        storage.setDeleted(storageUpdateBo.isDeleted());
        return storage;
    }

    public static StorageUpdateVO convertToStorageUpdateVO(Storage storage) {
        StorageUpdateVO storageUpdateVO = new StorageUpdateVO();
        storageUpdateVO.setId(storage.getId());
        storageUpdateVO.setKey(storage.getKey());
        storageUpdateVO.setName(storage.getName());
        storageUpdateVO.setType(storage.getType());
        storageUpdateVO.setSize(storage.getSize());
        storageUpdateVO.setUrl(storage.getUrl());
        storageUpdateVO.setAddTime(storage.getAddTime());
        storageUpdateVO.setUpdateTime(storage.getUpdateTime());
        storageUpdateVO.setDeleted(storage.getDeleted());
        return storageUpdateVO;
    }


    public static Storage convertToStorage(StorageDeleteBO storageDeleteBO) {
        Storage storage = new Storage();
        storage.setId(storageDeleteBO.getId());
        storage.setKey(storageDeleteBO.getKey());
        storage.setName(storageDeleteBO.getName());
        storage.setType(storageDeleteBO.getType());
        storage.setSize(storageDeleteBO.getSize());
        storage.setUrl(storageDeleteBO.getUrl());
        storage.setAddTime(storageDeleteBO.getAddTime());
        storage.setUpdateTime(new Date());
        storage.setDeleted(true);
        return storage;
    }

    public static List<Permission> convertToPermissionList(RolePermissionPostBO rolePermissionPostBO) {
        ArrayList<Permission> permissionsList = new ArrayList<>();
        List<String> permissions = rolePermissionPostBO.getPermissions();
        for (String permission : permissions) {
            permissionsList.add(new Permission(null, rolePermissionPostBO.getRoleId(),
                    permission, new Date(), new Date(), null));
        }
        return permissionsList;
    }


    public static StorageCreateVO convertToStorageCreateVO(Storage storage) {
        StorageCreateVO storageCreateVO = new StorageCreateVO();
        storageCreateVO.setId(storage.getId());
        storageCreateVO.setKey(storage.getKey());
        storageCreateVO.setName(storage.getName());
        storageCreateVO.setType(storage.getType());
        storageCreateVO.setSize(storage.getSize());
        storageCreateVO.setUrl(storage.getUrl());
        storageCreateVO.setAddTime(String.valueOf(storage.getAddTime()));
        storageCreateVO.setUpdateTime(String.valueOf(storage.getUpdateTime()));
        return storageCreateVO;

    }
}
